package com.casic.ops.task.impl;


import com.casic.ops.configs.ApplicationContextUtil;
import com.casic.ops.consts.SchedulerPools;
import com.casic.ops.consts.SerialType;
import com.casic.ops.dao.SchedulerTaskDAO;
import com.casic.ops.entity.domain.SchedulerTaskDO;
import com.casic.ops.handler.scheduler.ITaskProcessor;
import com.casic.ops.service.api.SchedulerTaskRecordService;
import com.orion.utils.Valid;
import com.orion.utils.time.Dates;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName ops-platform
 * @Description 调度任务实现
 * @Author mianbin
 * @Date 2022/5/31 10:59
 * @Version 1.0
 **/
@Slf4j
public class SchedulerTaskImpl implements Runnable {

    private static SchedulerTaskDAO schedulerTaskDAO = ApplicationContextUtil.getBean(SchedulerTaskDAO.class);

    private static SchedulerTaskRecordService schedulerTaskRecordService = ApplicationContextUtil.getBean(SchedulerTaskRecordService.class);

    private Long id;

    public SchedulerTaskImpl(Long id) {
        this.id = id;
    }

    @Override
    public void run() {
        log.info("定制调度任务-触发 taskId: {}, time: {}", id, Dates.current());
        // 查询任务
        SchedulerTaskDO task = schedulerTaskDAO.selectById(id);
        Valid.notNull(task);
        // 创建任务
        Long recordId = schedulerTaskRecordService.createTaskRecord(id);
        // 执行任务
        ITaskProcessor processor = ITaskProcessor.with(recordId, SerialType.of(task.getSerializeType()));
        SchedulerPools.SCHEDULER_TASK_MAIN_SCHEDULER.execute(processor);
    }

}
